<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('文章类别新增')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">父级</label>
                    <div class="layui-input-block">
                        <ul class="dtree" data-id="-1" id="selectParent" name="parentId"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题555</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="title" placeholder="请输入标题" class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序123</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" lay-verif="sort" class="layui-input" />
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="class-save" lay-submit="" type="submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button class="pear-btn pear-btn-sm" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form','dtree'],function () {
        let MODULE_PATH = contextPath + "/article/class/";
        let form = layui.form;
        let dtree = layui.dtree;

        /**
         * 初始化 父级 的下拉菜单项
         */
        dtree.renderSelect({
            elem: "#selectParent",
            url: MODULE_PATH + "selectParent",
            method: 'post',
            selectInputName: {nodeId: "parentId", context: "title"},//selectParent_select_input  selectParent_select_nodeId
            skin: "layui",
            dataFormat: "list",
            record: true,//开启参数里面增加 recordData，这里存放的是当前数据库的数据
            response: {treeId: "id", parentId: "parentId", title: "title"},  //修改response中返回数据的定义
            selectInitVal: "0"
        });

        form.on('submit(class-save)', function (data) {
            /**
             * data 包含 icon,#select的id_select_input
             * title(selectInputName: {nodeId: "parentId", context: "title"}这里的title)
             * 以及form里各个输入框 name 的
             */

            data.field.selectParent_select_input = undefined;
            $.ajax({
                url: MODULE_PATH + 'save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.treetable.reload("#class-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

    });


</script>
</body>
</html>